<?php
namespace app\index\controller;

use QL\QueryList;

class Index extends \think\Controller
{
    protected $temp_path;
    public function initialize()
    {
        $this->temp_path = env('root_path').'/template/'.site_config('site_theme').'/';
    }
    public function click()
    {
        $data['url']= empty($_SERVER["HTTP_REFERER"])?false:$_SERVER["HTTP_REFERER"];
        if($data['url']){
            $data['time_add']=time();
            $data['ip']=\request()->ip();
            db('click')->insertGetId($data);
            echo "console.log('ok')";
            exit();
        }
        
    }
    
    function cs(){
        $data=model('cate')->select()->toArray();
        var_dump($data);
        $data=list_to_tree($data,'id','pid');
        dump($data);
    }
    
    public function index()
    {
        
        if (!file_exists(env('root_path').'/static/upload/install/install.lock')) {
            return \redirect('/index.php/install');
        }
        
        $seo['seo_title']=\site_config('seo_title');
        $seo['seo_key']=\site_config('seo_key');
        $seo['seo_desc']=\site_config('seo_desc');
        $this->assign('seo', $seo);
        return view($this->temp_path.'index.html');
    }
    public function list()
    {
         
        
        $map=[];
        if (input('id')) {
            $map['id']=input('id');
        } else {
            $map['letter']=input('name');
        }
        $cate=db('cate')->where($map)->find();
        if (empty($cate)) {
            \abort(404);
        }
        $this->assign('cid', $cate['id']);
    
        $seo['seo_title']=($cate['seo_title']?$cate['seo_title']:$cate['name']).'-'.\site_config('site_name');
        $seo['seo_key']=$cate['seo_key']?$cate['seo_key']:$cate['name'];
        $seo['seo_desc']=$cate['seo_desc']?$cate['seo_desc']:$cate['intro'];
        $this->assign('seo', $seo);
        if ($cate['mid']==1) {
            $this->assign('cate', $cate);
            return view($this->temp_path.($cate['temp_list']?$cate['temp_list']:'list_page.html'));
        }
        $this->assign('cate', $cate);
        return view($this->temp_path.($cate['temp_list']?$cate['temp_list']:'list.html'));
    }
    public function read()
    {
        $post=db('post')->where('id', input('id'))->find();
        foreach (db('tag')->select() as $tag) {
            $post['body']=\str_replace($tag['name'], "<a href='{$tag['url']}' title='{$tag['name']}' >{$tag['name']}</a>", $post['body']);
        }
        $cate=db_find('cate', 'id='.$post['cid']);
        if (empty($post)) {
            \abort(404);
        }
        $seo['seo_title'] = ($post['seo_title']?$post['seo_title']:$post['name']).'-'.\site_config('site_name');
        $seo['seo_key']	  = $post['seo_key']?$post['seo_key']:$post['name'];
        $seo['seo_desc']  = $post['seo_desc']?$post['seo_desc']:$post['intro'];
        
        $map[]=['cid','=',$post['cid']];
        $map[]=['id','<',$post['id']];
        $post['prev']=db('post')->where('cid', $post['cid'])->where('id', '<', $post['id'])->order('id')->find();
        $post['next']=db('post')->where('cid', $post['cid'])->where('id', '>', $post['id'])->order('id')->find();
        $this->assign('seo', $seo);
        $this->assign('post', $post);
        $this->assign('cid', $post['cid']);
        $this->assign('cate', $cate);
        return view($this->temp_path.($cate['temp_read']?$cate['temp_read']:'read.html'));
    }
}
